package com.nantong.admin.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.nantong.admin.domain.dto.LoginDTO;
import com.nantong.admin.domain.dto.LoginResultDTO;
import com.nantong.admin.service.ISysAuthService;
import com.nantong.common.core.domain.R;
import com.nantong.common.core.domain.model.LoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 认证授权
 *
 * @author nantong
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/system/auth")
@Tag(name = "认证授权", description = "认证授权相关接口")
public class SysAuthController {

    private final ISysAuthService authService;

    /**
     * 用户登录
     */
    @SaIgnore
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    public R<LoginResultDTO> login(@Validated @RequestBody LoginDTO loginDTO) {
        LoginResultDTO result = authService.login(loginDTO);
        return R.ok("登录成功", result);
    }


    /**
     * 刷新令牌
     */
    @PostMapping("/refresh")
    @Operation(summary = "刷新令牌", description = "刷新用户令牌有效期")
    public R<Void> refresh() {
        LoginUser loginUser = authService.getLoginUser();
        authService.refreshToken(loginUser);
        return R.ok("刷新成功");
    }

    /**
     * 用户登出
     */
    @PostMapping("/logout")
    @Operation(summary = "用户登出", description = "用户登出接口")
    public R<Void> logout() {
        authService.logout();
        return R.ok("退出成功");
    }

}
